package com.credithc.signature.core.util;

import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * 描述：阿里云上传Util,支持同名覆盖<br/>
 * 作者：sunyang
 * 创建：2019/6/4 16:39
 * 版本：v1.0.0<br/>
 */
@Slf4j
public class AliOssUtil {

    /**
     * 创建客户端
     * @return
     */
    private static OSSClient getConn(String endpoint, String ossKey ,String ossSecret, String bucket){
        // 创建ClientConfiguration实例，按照您的需要修改默认参数
        ClientConfiguration conf = new ClientConfiguration();
        // 开启支持CNAME选项
        conf.setSupportCname(true);
        // 创建OSSClient实例
        return new OSSClient(endpoint, ossKey, ossSecret, conf);
    }

    /**
     * 删除文件
     * @param fileName
     */
    public static void deleteFile(String fileName, String endpoint, String ossKey , String ossSecret,String bucket) {
        OSSClient client = getConn(endpoint, ossKey, ossSecret, bucket);
        client.deleteObject(bucket, fileName);
        // 关闭client
        client.shutdown();
    }

    /**
     * 保存文件
     * @param file
     * @param fileName
     */
    public static void saveFile(File file, String fileName, String endpoint, String ossKey ,
                                  String ossSecret, String bucket) {
        OSSClient client = getConn(endpoint, ossKey, ossSecret, bucket);
        client.putObject(bucket, fileName, file);
        // 关闭client
        client.shutdown();
    }

    /**
     * 获取文件
     * @param fileName
     * @return
     * @throws IOException
     */
    public static byte[] getFile(String fileName, String endpoint, String ossKey , String ossSecret, String bucket) throws Exception {
        OSSClient client = getConn(endpoint, ossKey, ossSecret, bucket);
        OSSObject object = client.getObject(bucket, fileName);
        InputStream objectContent = object.getObjectContent();
        try {
            return ByteUtil.readLoopData(objectContent);
        } catch (Exception e) {
            log.error("读取文件{}失败", fileName, e);
            throw new RuntimeException(e);
        } finally {
            client.shutdown();
        }
    }

    /**
     * 保存文件
     * @param file
     * @param fileName
     * @param contentType
     * @return
     */
    public static void saveFile(File file, String fileName, String contentType, String realName, String endpoint,
                                  String ossKey , String ossSecret, String bucket) {
        OSSClient client = getConn(endpoint, ossKey, ossSecret, bucket);
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentType(contentType);
        client.putObject(bucket, fileName, file, meta);
        // 关闭client
        client.shutdown();
    }

    /**
     * 保存文件,仅支持PDF
     * @param inputStream
     * @param fileName
     * @return
     */
    public static void saveFile(InputStream inputStream, String fileName, String endpoint, String ossKey ,
                                  String ossSecret, String bucket) {
        OSSClient client = getConn(endpoint, ossKey, ossSecret, bucket);
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentType("application/pdf");
        client.putObject(bucket, fileName, inputStream, meta);
        // 关闭client
        client.shutdown();
    }
}

